Code
library(tidyverse)
library(here)
library(sf)
library(tmap)
library(spatstat)This analysis looks at inland oil spills across the state of California in 2008, as documented by the California Department of Fish and Wildlife Office of Spill Prevention and Response (OSPR).
Data source: California Department of Fish and Wildlife. Oil Spill Incident Tracking. Published Jul 29 2009. Last updated Oct 24 2023. Data download available here.
This analysis had three main goals:
Visualize the locations of 2008 oil spills across the state of California
Identify which counties in the state had the highest number of oil spills that year
Assess whether oil spills are spatially clustered or randomly spaced across the state
Read in California counties shapefile
Read in CSV file containing oil spill data
Convert oil spill dataframe to simple features object
Check the CRS of the counties file; set oil spill sf to same CRS
Create map of California with points denoting oil spills
Make map interactive so the user can zoom and click on points
Spatial join the counties with the oil spill points
Calculate the number of oil spills in each county
Visualize on a static choropleth map to identify counties with highest oil spill incidences
library(tidyverse)
library(here)
library(sf)
library(tmap)
library(spatstat)### Read in California counties
ca_counties_sf <- read_sf(here('data', 'ca_counties'), layer = 'CA_Counties_TIGER2016') %>%
janitor::clean_names() %>%
select(name)
### Read in oil spill csv
oil_df <- read_csv(here('data', 'oil_spill.csv')) %>%
janitor::clean_names()### Convert lat-long oil dataframe to sf
oil_sf <- oil_df %>%
drop_na(x, y) %>%
st_as_sf(coords = c("x", "y"))### Check CRS of counties file
# st_crs(ca_counties_sf) # "EPSG", 3857
### Set oil sf CRS to CRS of CA counties
st_crs(oil_sf) <- 3857
### Check CRSs are equal
# st_crs(oil_sf) == st_crs(ca_counties_sf)### set the viewing mode to interactive
tmap_mode(mode = 'view')
tm_shape(ca_counties_sf) +
tm_fill(col = "white") +
tm_shape(oil_sf) +
tm_dots(col = "darkblue")